var t = new (require("../../libs/qqmap-wx-jssdk.min.js"))({
    key: "NEHBZ-IP7R4-WCAUL-DC5EM-2O652-MVBAP"
}), a = require("../../utils/config.js");

Page({
    data: {
        address: "",
        latitude: 0,
        longitude: 0,
        imageList: [],
        memo: "",
        isPhoto: !1,
        isSign: !1,
        isMemo: !1,
        task: {},
        userTaskId: ""
    },
    onLoad: function(e) {
        var s = this;
        e.id && (wx.showLoading({
            mask: !0
        }), s.setData({
            userTaskId: e.id
        }), wx.request({
            url: a.urlList.API_TASK_GET_MY_DETAIL,
            method: "GET",
            data: {
                userId: wx.getStorageSync("userId"),
                id: e.id
            },
            success: function(a) {
                wx.hideLoading(), 200 == a.data.status ? (s.setData({
                    task: a.data.data.task
                }), s.setData({
                    isPhoto: s.data.task.acceptanceRequirements.indexOf("照片") >= 0,
                    isMemo: s.data.task.acceptanceRequirements.indexOf("文字报告") >= 0,
                    isSign: s.data.task.acceptanceRequirements.indexOf("签到") >= 0,
                    address: s.data.task.taskReport.mobileSign ? s.data.task.taskReport.mobileSign.address : "",
                    memo: s.data.task.taskReport.mobileSign ? s.data.task.taskReport.mobileSign.reson : "",
                    longitude: s.data.task.taskReport.mobileSign ? s.data.task.taskReport.mobileSign.lng : 0,
                    latitude: s.data.task.taskReport.mobileSign ? s.data.task.taskReport.mobileSign.lat : 0,
                    imageList: s.data.task.taskReport.imageList
                }), 1 == s.data.task.userTaskStatus && s.data.task.acceptanceRequirements.indexOf("签到") >= 0 && wx.getLocation({
                    type: "gcj02",
                    success: function(a) {
                        t.reverseGeocoder({
                            location: {
                                latitude: a.latitude,
                                longitude: a.longitude
                            },
                            success: function(t) {
                                console.log(t), s.setData({
                                    address: t.result.formatted_addresses.recommend
                                });
                            },
                            fail: function(t) {
                                console.log(t);
                            }
                        }), console.log(JSON.stringify(a)), s.setData({
                            latitude: a.latitude,
                            longitude: a.longitude,
                            markers: [ {
                                id: 0,
                                iconPath: "../../images/ic_position.png",
                                longitude: a.longitude,
                                latitude: a.latitude,
                                width: 30,
                                height: 30
                            } ]
                        });
                    }
                })) : wx.showToast({
                    title: a.data.message,
                    icon: "none"
                });
            }
        }));
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {
        var t = this, a = getCurrentPages(), e = a[a.length - 2], s = new Object();
        s.lng = t.data.longitude, s.lat = t.data.latitude, s.address = t.data.address, s.reson = t.data.memo;
        var i = wx.getStorageSync("serviceProviderId");
        s.serviceProviderId = i, s.userId = wx.getStorageSync("userId");
        var o = e.data.taskDetail;
        o.task.taskReport.mobileSign = s, o.task.taskReport.imageList = t.data.imageList, 
        e.setData({
            taskDetail: o
        }), console.log(s);
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    previewImage: function(t) {
        var a = this;
        1 == a.data.task.userTaskStatus ? wx.showActionSheet({
            itemList: [ "浏览", "删除" ],
            success: function(e) {
                0 == e.tapIndex ? wx.previewImage({
                    current: t.target.dataset.src,
                    urls: [ t.target.dataset.src ]
                }) : 1 == e.tapIndex && wx.showModal({
                    title: "系统提示",
                    content: "是否删除该图片？",
                    success: function(e) {
                        if (e.confirm) {
                            var s = [];
                            a.data.imageList.forEach(function(a, e) {
                                a.id != t.target.dataset.id && s.push(a);
                            }), a.setData({
                                imageList: s
                            });
                        }
                    }
                });
            }
        }) : wx.previewImage({
            current: t.target.dataset.src,
            urls: [ t.target.dataset.src ]
        });
    },
    chooseImage: function() {
        var t = this;
        wx.chooseImage({
            count: 1,
            success: function(e) {
                console.log(e), wx.showLoading({
                    mask: !0
                }), wx.uploadFile({
                    url: a.urlList.API_TOOLS_UPLOAD_FILE,
                    filePath: e.tempFilePaths[0],
                    name: "file",
                    success: function(a) {
                        wx.hideLoading();
                        var e = JSON.parse(a.data);
                        console.log(e), 200 == e.status ? t.setData({
                            imageList: t.data.imageList.concat({
                                id: e.data.fileId
                            })
                        }) : wx.showToast({
                            title: a.data.message,
                            icon: "none"
                        });
                    }
                });
            }
        });
    },
    getMemo: function(t) {
        this.setData({
            memo: t.detail.value
        });
    },
    finished: function() {
        var t = this, e = wx.getStorageSync("userId"), s = new Object();
        if (s.files = "", s.userId = wx.getStorageSync("userId"), s.userTaskId = t.data.userTaskId, 
        s.serviceProviderId = wx.getStorageSync("serviceProviderId"), t.data.task.acceptanceRequirements.indexOf("照片") >= 0) {
            if (!t.data.imageList || 0 == t.data.imageList.length) return void wx.showToast({
                title: "请完成任务汇报",
                icon: "none"
            });
            t.data.imageList && t.data.imageList.forEach(function(t, a) {
                s.files += t.id + ",";
            });
        }
        if (t.data.task.acceptanceRequirements.indexOf("签到") >= 0) {
            if (!t.data.address) return void wx.showToast({
                title: "请完成任务汇报",
                icon: "none"
            });
            s.lng = t.data.lng, s.lat = t.data.lat, s.address = t.data.address;
        }
        if (t.data.task.acceptanceRequirements.indexOf("文字报告") >= 0) {
            if (!t.data.memo) return void wx.showToast({
                title: "请完成任务汇报",
                icon: "none"
            });
            s.reson = t.data.memo;
        }
        console.log(s), wx.showLoading({
            mask: !0
        }), wx.request({
            url: a.urlList.API_MOBILESIGN_SIGN,
            method: "POST",
            data: s,
            header: {
                contentType: "application/json"
            },
            success: function(s) {
                if (200 == s.data.status) {
                    var i = {
                        userId: e,
                        id: t.data.userTaskId
                    };
                    wx.request({
                        url: a.urlList.API_TASK_FINISH_TASK,
                        method: "POST",
                        data: i,
                        header: {
                            contentType: "application/json"
                        },
                        success: function(t) {
                            wx.hideLoading(), 200 == t.data.status ? (wx.showToast({
                                title: "完成任务提交成功"
                            }), wx.navigateBack()) : wx.showToast({
                                title: t.data.message,
                                icon: "none"
                            });
                        }
                    });
                } else 415 == s.data.status ? (wx.hideLoading(), wx.showModal({
                    title: "系统提示",
                    content: s.data.message,
                    cancelText: "下次再说",
                    confirmText: "立即完善",
                    success: function(t) {
                        t.confirm && wx.navigateTo({
                            url: "/pages/my/editBank"
                        });
                    }
                })) : 416 == s.data.status ? (wx.hideLoading(), wx.showModal({
                    title: "系统提示",
                    content: s.data.message,
                    cancelText: "下次再说",
                    confirmText: "实名认证",
                    success: function(t) {
                        t.confirm && wx.navigateTo({
                            url: "/pages/my/realNameAuthentication"
                        });
                    }
                })) : wx.showToast({
                    title: s.data.message,
                    icon: "none"
                });
            }
        });
    }
});